# -*- utf-8 -*-


import requests,os
from bs4 import BeautifulSoup
import time
import re
from requests.adapters import HTTPAdapter
import socket
from lxml import etree
import datetime

need_time = datetime.datetime.now().strftime('%Y-%m-%d')

socket.setdefaulttimeout(150)
print('待发货=7\n'
      '已发货=8\n'
      '售后退货=16')

print('请输入需要导出的订单代码：')
status = input()


s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=2))#设置重试次数为3次
s.mount('https://', HTTPAdapter(max_retries=2))

headers0 = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
}

login_url = 'https://dg.slpzb.com/portal/login/?next=/portal/' #登录页面
getkey = s.get(login_url, headers=headers0)

for line in getkey:
    try:
        line = line.decode()
        if 'csrfmiddlewaretoken' not in line: continue
        else:
            key1 = line.split('value=\'')[1].split('\'')[0] #获得登陆页面的token
            #print(key1)

        headers1 = {
            'authority': 'dg.slpzb.com',
            'method': 'POST',
            'path': '/portal/login/?next=/portal/',
            'scheme': 'https',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'max-age=0',
            'content-length': '137',
            'content-type': 'application/x-www-form-urlencoded',
          #  'cookie': '',
            'origin': 'https://dg.slpzb.com',
            'referer': 'https://dg.slpzb.com/portal/login/?next=/portal/',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
        }

        date1 = {
            'username': '账号',
            'password': '密码',
            'csrfmiddlewaretoken': key1
        }


        link1 = 'https://dg.slpzb.com/portal/login/?next=/portal/'
        login = s.post(link1, data=date1, headers=headers1)
        #print(login.content)

    #上面代码登录完成
        link2 = 'https://dg.slpzb.com/portal/order/order/' #订单页面
        getkey2 = s.get(link2, headers=headers0)
        for line1 in getkey2:
            try:
                line1 = line1.decode()
                if 'csrfmiddlewaretoken' not in line1: continue
                else:
                    key2 = line1.split('value=\'')[1].split('\'')[0] #获得订单页面的token
                    #print(key2)

                    link = 'https://dg.slpzb.com/portal/order/order/?drf__last_modified__gte=%s&drf__last_modified__lte=%s&fendian=23&item_status=%s' % (need_time, need_time, status)
                    #导出最近更新的订单的接口

                    headers = {
                        'authority': 'dg.slpzb.com',
                        'method': 'POST',
                        #'path': '/portal/order/order/?drf__last_modified__gte=2020-09-01&drf__last_modified__lte=2020-09-01&fendian=23&item_status=7',
                        'scheme': 'https',
                        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'zh-CN,zh;q=0.9',
                        'cache-control': 'max-age=0',
                        'content-length': '2649',
                        'content-type': 'application/x-www-form-urlencoded',
                      #  'cookie': 'Hm_lvt_a03a44d83b5005aa127584aad7d05d9d=1598855071,1598857321; csrftoken=7RNzH9WvBt3Rp5cwKSOOyokvSrp0rPbeD00MUGG6ZWNVttg12chHqdKLbqYjh4eM; sessionid=jlh9d2y1xl3xjauopqyntkrnm0csjxyj',
                        'origin': 'https://dg.slpzb.com',
                        'referer': link,
                        'sec-fetch-dest': 'document',
                        'sec-fetch-mode': 'navigate',
                        'sec-fetch-site': 'same-origin',
                        'sec-fetch-user': '?1',
                        'upgrade-insecure-requests': '1',
                        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
                    }


                    dates = {
                        'csrfmiddlewaretoken': key2,
                        'action': 'export_admin_action',
                        'select_across': '1',
                        'file_format': '0',
                        'index': '0',
                        '_selected_action': '0'
                    }


                    try:
                        response_1 = s.post(link, data=dates, headers=headers)
                        # path = 'D:/导单'
                        # if not os.path.exists(path):
                        #     os.mkdir(path)
                        f1 = open('订单代码%s.csv' % status, 'wb')
                        f1.write(response_1.content)
                        f1.close()

 #------------------------------------下载完记一下
                        print('下载完成')
                    except Exception as e:
                        continue
            except Exception as e:
                continue
    except Exception as e:
        continue
